package xyz.sicau.bootrabbitmq.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

// 同时给两个队列发消息，这里用了 log4j来输出日志，swagger实现rust风格参数设置
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message) {
        log.info("{} 发送: {}", new Date().toString(), message);
        rabbitTemplate.convertAndSend("X", "XA", "消费来着XA（10s）的队列" + message);
        rabbitTemplate.convertAndSend("X", "XB", "消费来着XB（40s）的队列" + message);
    }

    @GetMapping("/sendMsgWithTTL/{message}/{ttl}")
    public void sendMsg(@PathVariable String message, @PathVariable String ttl) {
        log.info("{} 发送: {} ,TTL为{}", new Date(), message, ttl);
        // 发送时设置TTL
        rabbitTemplate.convertAndSend("X", "XC", message, msg -> {
            msg.getMessageProperties().setExpiration(ttl);
            return msg;
        });
    }
}
